package com.java1412.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author java1412
 * @since 2023-02-20 10:55
 */
@Service
public class HelloService {

    // SERVICE-HI：服务注册eureka中心的应用名称
    private static final String URL = "http://SERVICE-HI";

    // 注册restTemplate
    @Autowired
    private RestTemplate restTemplate;

    // 指定熔断的方法,fallbackMethod属性：指定的熔断降级的方法
    @HystrixCommand(fallbackMethod = "hiError")
    public String hiService(String userName) {
        return restTemplate.getForObject(URL + "/hi?userName=" + userName, String.class);
    }

    public String hiError(String userName) {
        return "hi," + userName + ", sorry,error!";
    }
}
